package com.library.controller;


import com.library.bean.Admin;
import com.library.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
public class RegisterController {
    @Autowired
    private RegisterService registerService;


    @RequestMapping(value = "api/register", method = RequestMethod.POST)
    @ResponseBody
    public Object register(HttpServletRequest request){
        String uname=request.getParameter("uname");
        long id = Long.parseLong(request.getParameter("id"));
        String pwd = request.getParameter("pwd");
        HashMap<String, String> res = new HashMap<>();
        System.out.println(uname);
        //System.out.println(pwd);
        Admin admin=new Admin();
        admin.setAdminId(id);
        admin.setUsername(uname);
        admin.setPassword(pwd);
        registerService.addAdmin(admin);
        request.getSession().setAttribute("admin", admin);
        res.put("stateCode", "1");
        res.put("msg", "注册成功！");
        return res;
    }

}
